﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using UtilZ.Dotnet.Ex.Log;

namespace DotnetWinFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }

            
            //NotificationMessageType
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            logControl1.AddLog(e.Item.Content, e.Item.Level);

            //string str;
            //try
            //{
            //    str = string.Format("{0} {1}", DateTime.Now, e.Item.Content);
            //}
            //catch (Exception ex)
            //{
            //    str = ex.Message;
            //}

            //logControl.AddLog(str, e.Item.Level);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            logControl1.Clear();
        }


        private CusDrawControl _control = null;
        private void btnTest_Click(object sender, EventArgs e)
        {
            if (this._control != null)
            {
                return;
            }

            var control = new CusDrawControl();
            control.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            control.Location = new Point(20, 30);
            //panel2.Controls.Add(control);
            panel1.Controls.Add(control);
            //opacityPanel1.Controls.Add(control);
            this._control = control;


        }

        private void btnStartAnimal_Click(object sender, EventArgs e)
        {
            if (this._control != null)
            {
                this._control.StartAnimal();
            }
        }

        private void btnStopAnimal_Click(object sender, EventArgs e)
        {
            if (this._control != null)
            {
                this._control.StopAnimal();
            }
        }

        private void btnClearRegion_Click(object sender, EventArgs e)
        {
            if (this._control != null)
            {
                this._control.Clear();
            }
        }

        /// <summary>
        /// 取得一个图片中非透明色部分的区域。
        /// </summary>
        /// <param name="Picture">取其区域的图片。</param>
        /// <param name="transparentColor">透明色。</param>
        /// <returns>图片中非透明色部分的区域</returns>
        private Region BmpRgn(Bitmap Picture, Color transparentColor)
        {
            int nWidth = Picture.Width;
            int nHeight = Picture.Height;
            Region rgn = new Region();
            rgn.MakeEmpty();
            bool isTransRgn;//前一个点是否在透明区
            Color curColor;//当前点的颜色
            Rectangle curRect = new Rectangle();
            curRect.Height = 1;
            int x = 0, y = 0;
            //逐像素扫描这个图片，找出非透明色部分区域并合并起来。
            for (y = 0; y < nHeight; ++y)
            {
                isTransRgn = true;
                for (x = 0; x < nWidth; ++x)
                {
                    curColor = Picture.GetPixel(x, y);
                    if (curColor == transparentColor || x == nWidth - 1)//如果遇到透明色或行尾
                    {
                        if (isTransRgn == false)//退出有效区
                        {
                            curRect.Width = x - curRect.X;
                            rgn.Union(curRect);
                        }
                    }
                    else//非透明色
                    {
                        if (isTransRgn == true)//进入有效区
                        {
                            curRect.X = x;
                            curRect.Y = y;
                        }
                    }//if curColor
                    isTransRgn = curColor == transparentColor;
                }//for x
            }//for y
            return rgn;
        }





        // This method will change the square button to a circular button by 
        // creating a new circle-shaped GraphicsPath object and setting it 
        // to the RoundButton objects region.
        //private void roundButton_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        private void roundButton_Paint()
        {
            Control control = opacityPanel1;
            System.Drawing.Drawing2D.GraphicsPath buttonPath = new System.Drawing.Drawing2D.GraphicsPath();

            // Set a new rectangle to the same size as the button's 
            // ClientRectangle property.
            System.Drawing.Rectangle newRectangle = control.ClientRectangle;

            // Decrease the size of the rectangle.
            newRectangle.Inflate(-10, -10);

            // Draw the button's border.
            var g = control.CreateGraphics();
            g.DrawEllipse(System.Drawing.Pens.Black, newRectangle);
            g.Dispose();
            //e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

            // Increase the size of the rectangle to include the border.
            newRectangle.Inflate(1, 1);

            // Create a circle within the new rectangle.
            buttonPath.AddEllipse(newRectangle);

            // Set the button's Region property to the newly created 
            // circle region.
            control.Region = new System.Drawing.Region(buttonPath);
        }


        private void XX(string msg)
        {
            
        }

    }
}
